/**
 * 
 */
package com.p2p.base.util.biz;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.p2p.base.model.Money;
import com.p2p.base.util.DateUtil;

/**
 * @author ruiqi
 *
 */
public class ModelViewUtil {

	public static <T> Map<String,Object> getMapModelView(T bean,List<String> nameList){
		Map<String,Object> data = new HashMap<String,Object>();
		Class c = bean.getClass();
		
		for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getDeclaredMethod(getMethodName);
				Object o=m.invoke(bean);
				data.put(name, o.toString());
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return data;
		
	}
	public static <T> List<Map<String,Object>> getMapModelViewList(List<T> beanList,List<String> nameList){
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(beanList==null||beanList.size()==0){
			return list;
		}
        Class c =beanList.get(0).getClass();
        Map<String,Method> mothodMap = new HashMap<String,Method>();
       

        for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getDeclaredMethod(getMethodName);
				mothodMap.put(name, m);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		for(T bean:beanList){
			Map<String,Object> data = new HashMap<String,Object>();
			list.add(data);
			for(Map.Entry<String, Method> entry:mothodMap.entrySet()){

				try {
					Object o=entry.getValue().invoke(bean);
					String viewStr = null;
					if(o!=null){
						if(o instanceof Date){
							viewStr = DateUtil.getDateTime(o);
						}else{
							viewStr = o.toString();
						}
					}
					
					data.put(entry.getKey(), viewStr);
					
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		return list;
		
	}
	
	
	/**
	 * List<Object> 转 List<Map<String,Object>>,并可以指定需要的字段
	 * 
	 * @param beanList
	 * @param nameList
	 * @return
	 */
	public static <T> List<Map<String,Object>> getMapModelObjectList(List<T> beanList,List<String> nameList){
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(beanList==null||beanList.size()==0){
			return list;
		}
        Class c =beanList.get(0).getClass();
        Map<String,Method> mothodMap = new HashMap<String,Method>();
       

        for(String name:nameList){
			String first = name.substring(0, 1);
			String getMethodName  = "get"+name.replaceFirst(first, first.toUpperCase());
			try {
				Method m = c.getDeclaredMethod(getMethodName);
				mothodMap.put(name, m);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		for(T bean:beanList){
			Map<String,Object> data = new HashMap<String,Object>();
			list.add(data);
			for(Map.Entry<String, Method> entry:mothodMap.entrySet()){

				try {
					Object o=entry.getValue().invoke(bean);
					
					
					data.put(entry.getKey(), o);
					
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
		return list;
		
	}
	
	
	public static class testModel{
		private int id;
		
		private String name;
		
		private Money amount = new Money();

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Money getAmount() {
			return amount;
		}

		public void setAmount(Money amount) {
			this.amount = amount;
		}
		
		
	}
	public static void main(String[] args) {
		testModel vo = new testModel();
		vo.setId(4);
		vo.setName("test1");
		vo.setAmount(new Money(1,9));
		
		List<String> nameList= new ArrayList<String>();
		nameList.add("id");
		nameList.add("name");
		nameList.add("amount");
		Map<String,Object> data = getMapModelView(vo,nameList);
		
		String str = JSON.toJSONString(data);
		System.out.println(str);
		
		List<testModel> beanList = new ArrayList<testModel>();
		beanList.add(vo);
		List<Map<String,Object>> data2 = getMapModelViewList(beanList,nameList);
		String str2 = JSON.toJSONString(data2);
		System.out.println(str2);
		
		/*List<Map> beanList = new ArrayList<Map>();
		Class c =null;
		Method method;
		try {
			method = beanList.getClass().getMethod("get",int.class);
			c = method.getReturnType();
			Type[] tarray = method.getReturnType().getGenericInterfaces();
			for(Type t:tarray){
				System.out.println(t.getClass().toString());
			}
		} catch (NoSuchMethodException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SecurityException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
		System.out.println(c);*/
	}
}
